package com.umeox.ota.engine;


import com.umeox.ota.data.constant.Constant;
import com.abupdate.fotainject.FotaInjector;
import com.abupdate.iot_libs.data.constant.OtaConstants;
import com.abupdate.iot_libs.data.remote.PolicyMapInfo;
import com.abupdate.iot_libs.interact.callback.IParsePolicyListener;
import com.abupdate.iot_libs.utils.SPFTool;
import com.abupdate.sdk_annotation.Inject;
import com.abupdate.trace.Trace;

/**
 * @author fighter_lee
 * @date 2018/2/2
 * <p>
 * 自定义仅wifi策略
 */
public class PolicyDLWifi implements IParsePolicyListener {
    public static final String TAG = PolicyDLWifi.class.getSimpleName();

    private static boolean CHOOSE_DL = false;

    @Inject
    com.abupdate.iot_libs.data.remote.VersionInfo versionInfo;

    @Override
    public boolean doParse() {
        FotaInjector.inject(this);
        if (CHOOSE_DL) {
            Trace.d(TAG, "doParse() CHOOSE_DL is true");
            CHOOSE_DL = false;
            return false;
        }
        boolean serverConfig = getServerConfig();
        Trace.d(TAG, "doParse() wifi condition server config: %s", serverConfig);
        if (!serverConfig) {
            boolean localConfig = SPFTool.getBoolean(Constant.SP_ONLY_WIFI_DOWNLOAD, false);
            Trace.d(TAG, "doParse() wifi condition local config: %s", localConfig);
            return localConfig;
        }
        return true;
    }

    private boolean getServerConfig() {
        PolicyMapInfo wifiInfo = versionInfo.policyHashMap.get(OtaConstants.KEY_DOWNLOAD_WIFI);
        if (wifiInfo != null) {
            return "required".equals(wifiInfo.key_value);
        }
        return false;
    }

    public static void chooseToDL() {
        Trace.d(TAG, "chooseToDL() change into true");
        CHOOSE_DL = true;
    }
}
